﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Store.Bll;
using Store.Model.Models;
using Store.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Store.Server.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class PromotionsController : ControllerBase
    {
        private PromotionBLL bll;
        public PromotionsController(PromotionBLL _bll)
        {
            bll = _bll;
        }
        [HttpGet]
        //显示
        public IActionResult GetPromotions(string Ptitle = "", int Pstate = 2,int pageIndex=1,int pageSize=5)
        {
            var data = bll.GetPromotions(Ptitle, Pstate);
            var pagelist = data.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Ok(new { total = data.Count(), data = pagelist });
        }
        [HttpPost]
        //添加
        public int AddPromotions(Promotion pro)
        {
            return bll.AddPromotions(pro);
        }
        [HttpDelete]
        //删除
        public int DelPromotions(int id)
        {
            return bll.DelPromotions(id);
        }
        [HttpPut]
        //修改
        public int UpdatePromotions(Promotion pro)
        {
            return bll.UpdatePromotions(pro);
        }
        //显示商品
        [HttpGet]
        public List<CommodityModel> GetCommodities()
        {
            return bll.GetCommodities();
        }
        //删除商品
        [HttpDelete]
        public int DeleteCommodity(int id)
        {
            return bll.DeleteCommodity(id);
        }
    }
}
